package edu.mit.media.fv.blocks
{
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.icons.*;
	
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.*;
	
	public class SpiderBlock extends Block
	{
		
		private var collapsed:Boolean = true;
		private var satillites:Array = new Array();
		private var center:Sprite;
		
		public function SpiderBlock(model:Model, controller:FluidSpace, icon:DisplayObject = null, text:String = "", color:uint = 0xFFFFFF)
		{
			super(model, controller, null, text, color);
			this.center = new Sprite();
			this.addChild(center);
			this.addChildAt(center, 0);
			this.center.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
			this.center.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
			
			this.icon = icon;
			
			this.center.addChild(this._label);
		}
		public override function set icon(icon:DisplayObject):void{
			if(this._icon != null){
				this.center.removeChild(this._icon);
			}
			this._icon = icon;
			if(icon != null){
				this.center.addChild(icon);
			}
			
		}
		public override function draw():void{
			this.reform();
		}
		private function dropSatellite(event:MouseEvent):void{
			var sat:Satellite = Satellite(event.currentTarget);
			sat.stopDrag();
			var d:int = Math.sqrt(Math.pow((sat.x - this.radius),2)+Math.pow((sat.y - this.radius),2));
			if (d>this.radius*3){
				GroupModel(this.model).removeModel(sat.model);
				var data:Object = new Object();
				data.command = FluidConstants.REQUEST_REMOVE_GROUPUSER;
				data.user_id = sat.model.id;
				data.id = this.model.id;
				var e:FVNetworkEvent = new FVNetworkEvent(data);
				FluidEventHandler.dispatchEvent(e);
		
			}else{
				sat.snapBack();
			}
		}
		private function dragSatellite(event:MouseEvent):void{
			var sat:Satellite = Satellite(event.currentTarget);
			sat.startDrag();
		}
		private function reform():void{
			trace("reforming spider block");
			for(var j:int = 0; j<satillites.length; j++){
				this.removeChild(Sprite(this.satillites[j]));
			}
			
			satillites = new Array();
			var models:Array = GroupModel(this.model).users;
			var theta:Number = 0;
			if(models.length > 0){
				theta = 2*Math.PI/models.length;
			}
			for(var i:int = 0; i<models.length; i++){
				var model:Model = models[i];
				var satRadius:int = this.radius/2;
				var satellite:Satellite = new Satellite(theta*i, model);
				satellite.x = this.radius;
				satellite.y = this.radius;
				this.satillites.push(satellite);
				this.addChildAt(satellite, 0);
				satellite.addEventListener(MouseEvent.MOUSE_DOWN, dragSatellite);
				satellite.addEventListener(MouseEvent.MOUSE_UP, dropSatellite);
			}
			if(!this.collapsed){
				trace("!collapsing block");
				for(i = 0; i<this.satillites.length; i++){
					satellite = Satellite(this.satillites[i]);
					satellite.setRadius(this.radius+25);
				}
			}
			
			this.drawCenter();
		}
		private function drawCenter():void{
			var type:String = GradientType.RADIAL;
			var colors:Array = [this.color, SHADOW];
			var alphas:Array = [1, 1];
			var ratios:Array = [0, 255];
			var spreadMethod:String = SpreadMethod.PAD;
			var interp:String = InterpolationMethod.LINEAR_RGB;
			var focalPtRatio:Number = 0;
			
			var matrix:Matrix = new Matrix();
			var boxWidth:Number = radius*4;
			var boxHeight:Number = radius*4;
			var boxRotation:Number = 0;//Math.PI/2; // 90˚
			var tx:Number = -this.radius*1.5;
			var ty:Number = -this.radius*1.5;
			matrix.createGradientBox(boxWidth, boxHeight, boxRotation, tx, ty);
			
			if(center != null){	
				center.graphics.clear();
				center.graphics.beginGradientFill(type, colors, alphas, ratios, matrix,
					spreadMethod,interp, focalPtRatio);
				center.graphics.drawCircle(radius, radius, radius-3);
				center.graphics.endFill();
				
				var glow:GlowFilter = new GlowFilter(0xFFFFFF,0.4,50,50);
				var bevel:BevelFilter = new BevelFilter();
				this.center.filters = [bevel];
			}
		}
		public override function moveBlock(newBlockContainer:BlockContainer):void{
			this.stopDrag();
			var stagePoint:Point = this.localToGlobal(new Point(0, 0));
			var containerPoint:Point = newBlockContainer.globalToLocal(new Point(stagePoint.x,stagePoint.y));
			this.x = Math.max(0, Math.min(containerPoint.x, newBlockContainer.containerWidth-this.diameter));
			this.y = Math.max(0, Math.min(containerPoint.y, newBlockContainer.containerHeight-this.diameter));
			if(this.blockContainer != null){
				this.blockContainer.removeBlock(this);
			}
			trace(this.x+", "+this.y);
			newBlockContainer.addBlock(this);
			if(Math.abs(this.dragPoint.x - this.x)<5){
				if(Math.abs(this.dragPoint.y - this.y)<5){
					trace("Note dragged: drag.x: "+(this.dragPoint.x - this.x)+", drag.y: "+(this.dragPoint.y - this.y));
					if(this.collapsed){
						trace("collapsing block");
						for(var i:int = 0; i<this.satillites.length; i++){
							var satellite:Satellite = Satellite(this.satillites[i]);
							satellite.show(this.radius+25);
						}
					}else{
						trace("!collapsing block");
						for(i = 0; i<this.satillites.length; i++){
							satellite = Satellite(this.satillites[i]);
							satellite.hide();
						}
					}
					this.collapsed = !this.collapsed;
				}
			}
		}
		
	}
}